<?php
defined('G_IN_SYSTEM')or exit('No permission resources.');
System::load_app_class('base');

class lottery extends base {

	public function __construct(){
		
		parent::__construct();
		$this->db = System::load_sys_class('model');
		
	}
	public function unserializeArray($array){
		foreach ($array as $key => $value) {
			if($value['q_uid']){
			$sql = "SELECT sum(moneycount) as money FROM `@#_member_go_record` where `uid`=".$value['q_uid']." and `shopid`=".$value['id']."";
			$money = $this->db->GetOne($sql);
			$array[$key]['moneycount']=$money['money'];
			}
			
		    if(array_key_exists('q_user',$value)){
			$array[$key]['q_user']=unserialize($value['q_user']);
			}
			}
		return $array;	
	}
	
	//获取将要开奖的商品
	public function lottery_shop_json(){
			$p = $_POST['page'] ? $_POST['page'] : 1;
			$end = 10;
			$start=($p-1)*$end;
		$time = time();
			$countSql = "SELECT * FROM(select qishu,id,zongrenshu,thumb,title,q_uid,q_user,q_user_code,q_end_time from `@#_shoplist` where `q_end_time` is not null and `q_end_time` > $time ORDER BY  `q_end_time` ASC)AS T1
			 UNION ALL
			SELECT * FROM(select qishu,id,zongrenshu,thumb,title,q_uid,q_user,q_user_code,q_end_time from `@#_shoplist` where `q_end_time` is not null and `q_end_time` <= $time ORDER BY `q_end_time` DESC) AS T2";
			$count = $this->db->GetList($countSql);
			
			$pagex=ceil(count($count)/$end);
			$infoSql = "SELECT * FROM(select qishu,id,zongrenshu,thumb,title,q_uid,q_user,q_user_code,q_end_time from `go_shoplist` where `q_end_time` is not null and `q_end_time` > $time ORDER BY  `q_end_time` ASC)AS T1
			 UNION ALL
			SELECT * FROM(select qishu,id,zongrenshu,thumb,title,q_uid,q_user,q_user_code,q_end_time from `go_shoplist` where `q_end_time` is not null and `q_end_time` <= $time ORDER BY `q_end_time` DESC) AS T2 limit $start,$end";
			$info = $this->db->GetList($infoSql);
			$info = $this->unserializeArray($info);
			$isNoData = false;
			if($pagex == $p || count($info) <= 1){
				$isNoData = true;
			}
			
			if ($info) {
				$data = array("isNoData"=>$isNoData,'info'=>$info);
				$this->success($data);
			}else{
				$this->failed("暂无开奖");
			}
			
	}


}

?>